#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tornado
import tornado.web
from utils import log
import json
from config import etc
from controls import data_collect_mysql
from utils.session_base import ApiGuest
from utils.session_mc import BaseHandler

class CollectHandler( BaseHandler ) :

    @ApiGuest
    def post( self ) :
        userid = self.ss_params['userid']
        authorid = self.ss_params['authorid']
        cityid = self.ss_params['cityid']
        journeyid = self.ss_params['journeyid']
        log.i( 'userid=%s, authorid=%s, cityid=%s, journeyid=%s' % (userid, authorid, cityid, journeyid) )
        if not (userid and authorid and cityid and journeyid) : 
            log.w('params fetching error')
            res = { 'idx':self.ss_idx, 'ret':1, 'msg':etc.err_500, 'res':{} }
            self.write( json.dumps(res) )
            self.finish()
            return
        item = {'userid': userid,
                'authorid': authorid,
                'cityid': cityid,
                'journeyid': journeyid}
        try :
            data_collect_mysql.add_collect_item( item )
            res = { 'idx':self.ss_idx, 'ret':0, 'msg':"收藏成功", 'res':{} }
            self.write( json.dumps(res) )
            self.finish()
            return
        except Exception, e :
            log.e( e )
            res = { 'idx':self.ss_idx, 'ret':1, 'msg':etc.err_500, 'res':{} }
            self.write( json.dumps(res) )
            self.finish()
            return
 
class ApiNotFoundHandler( BaseHandler ) : 
    def get( self ) :
        raise tornado.web.HTTPError( 503 )
    @tornado.web.asynchronous
    def post( self ):
        log.i( '503' )
        res = { 'idx':-1, 'ret':-5, 'msg':etc.err_no_api, 'res':{} }
        self.write( json.dumps(res) )
        self.finish()
